const app = getApp(), fetchApi = require("../../utils/httpPost.js"), ad = require("../../template/ad/adTemplate.js");
var common_Js = require("../../component/common.js");
import Toast from '../../component/vant/toast/toast';
import Dialog from '../../component/vant/dialog/dialog';
const CryptoJS = require("../../utils/aesUtil.js")
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    is_mer: 0,
    is_admin: 0,
    adBanner:[],
    hasBanner:false,
    isLogin:0,
    phone_num:'',
    face_url:'',
    nick_name:'',
    showSheet:false,
    estate_name:'',
    count0:1,
    count1:0,
    menuList:[],
    sys_user_name:'',
    estate_id: 0,
    isHasPaySlip:0,//是否有未签字的工资条
  },
  //事件处理函数
  bindViewTap: function() {
    // wx.navigateTo({ url: '../logs/logs' })
  },
  onLoad: function (options) {
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this;
    fetchApi.httpPost('/plan/manager/view', {
    }, function (res) {
      console.log("我的物业中心界面 初始化数据", res)
      if (res.data.code == 0) {
        that.data.mer_id = res.data.data.mer_id;
        that.data.merActions = res.data.data.merActions;
        that.data.estate_id = res.data.data.estate_id;
        that.data.isHasPaySlip = res.data.data.isHasPaySlip;
        that.setData({ 
          face_url: res.data.data.face_url,
          nick_name: res.data.data.nick_name,
          estate_name: res.data.data.estate_name,
          count0: res.data.data.count0,
          count1: res.data.data.count1,
          menuList: res.data.data.menuList,
          sys_user_name: res.data.data.sys_user_name,
        })
        if(that.data.isHasPaySlip == 1){//有未签字的工资条
          var message = "您有一条未签字的工资条"
          Dialog.confirm({
            title: '温馨提示',
            message,
            showConfirmButton:true,
            confirmButtonText:'签字确认',
          }).then(() => {
            that.goSign()
          })
          .catch(() => {
            Dialog.close();
          });
        }
      }
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    },function(){})
  },
  onShow: function () {
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    var is_mer = app.getStorageSync("is_mer");
    var is_admin = app.getStorageSync("is_admin");
    that.setData({
      isLogin: isLogin,
      is_mer: is_mer,
      is_admin: is_admin
    })
    app.globalData.userInfo = app.getStorageSync("userInfo");
    console.log('isLogin', isLogin)
    console.log('app.globalData.userInfo', app.globalData.userInfo)
    if (isLogin) {
      that.setData({
        userInfo: app.globalData.userInfo
      })
    }
    that.onHttpInit()
  },
  goSign(){
    wx.navigateTo({
      url: '/pagesProperty/pages/payslip/sign/index',
    })
  },
  goLogin:function(){
    wx.navigateTo({
      url: '/pages/login/index',
    })
  },
  getQrcode:function(e){
    console.log('pushToQrCode e', e)
    wx.navigateTo({
      url: '/pages/coupon/previewQrcode/index?qrcode_type=personal'
    })
  },
  scanCode: function (res) {
    console.log('scanCode res',res)
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    var estate_id = app.getStorageSync("estate_id");
    that.data.from_page = 'scancode'
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    } else {
      wx.scanCode({
        success: (res) => {
          console.log("扫码结果", res);
          var qrcontent = res.result;
          console.log('扫码结果',qrcontent)
          var json_param = {}
          var linkParam = qrcontent;
          var order_info_id = 0;
          var qrtype = '';
          var expire_time = 0;
          if(qrcontent.indexOf('https://') >= 0){
            var q = "?" + qrcontent.split("?")[1];
            json_param = common_Js.getQueryVariable(q);
            if(qrcontent.indexOf('event/point') >= 0){
              wx.navigateTo({ url: '/pagesProperty/pages/event/point/index?id=' + json_param.id})
            } else if(qrcontent.indexOf('asset/borrow') >= 0){//借用
              wx.navigateTo({ url: '/pagesProperty/pages/asset/borrow/index?id=' + json_param.id+'&from=scan'})
            } else if(qrcontent.indexOf('asset/check') >= 0){//盘点
              wx.navigateTo({ url: '/pagesProperty/pages/asset/check/index?id=' + json_param.id+'&from=scan'})
            }
          }else{
            if (qrcontent.indexOf('qrtype') < 0) {//加密过的数据
              qrcontent = CryptoJS.Decrypt(res.result)//解密
              // console.log('解密：qrcontent',qrcontent)
              json_param = JSON.parse(qrcontent);
              qrtype = json_param["qrtype"];
              linkParam = "qrtype=" + json_param["qrtype"] + "&id=" + json_param["id"];
              if(qrcontent.indexOf('orderInfo') >= 0){
                linkParam +="&order_info_id="+ json_param["id"];
                order_info_id = parseInt(json_param["id"]);
                expire_time = parseInt(json_param["expire_time"]);
              }
            }
            // console.log('linkParam',linkParam)
            var is_orderInfo_indexof = qrcontent.indexOf('orderInfo');
            if (is_orderInfo_indexof >= 0) {
              this.orderOp(linkParam,order_info_id,expire_time)
            }else{
              Toast.fail('该二维码不属于该操作');
            }
          }
        },
        fail: (res) => { console.log(res); }
      })
    }
  },
  orderOp:function(linkParam,info_id,expire_time){
    fetchApi.httpPost('/product/order/orderInfo', {
      info_id: info_id,
      reset_qr_code:0,
      expire_time:expire_time
    }, function (res) {
      console.log("订单详情信息", res)
      if (res.data.code == 0) {
        wx.navigateTo({ url: '/pagesMer/pages/validateQr/index?' + linkParam })
      } else {
        Toast.fail(res.data.msg);
      }
    }, function () { })
  },
  changeDefaultMer(){
    this.setData({
      showSheet: !this.data.showSheet
    })
  },
  onSelecSheett(e){
      console.log('onSelecSheett',e);
      var mer_id = e.detail.mer_id;
      this.changeDefaultMer()
      this.onHttpChangeDefault(mer_id)
  },
  onCloseSheet(e){
    console.log('onCloseSheet',e);
    this.setData({
      showSheet: false
    })
  },  
  /**
   * 获取页面数据的 http请求
   */
  onHttpChangeDefault: function (mer_id) {
    var that = this;
    fetchApi.httpPost('/mer/changeDefault', {
      mer_id: mer_id
    }, function (res) {
      console.log("onHttpChangeDefault 数据", res)
      if (res.data.code == 0) {
        that.onHttpInit();
      }
    },function(){})
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onHttpInit()
  },
})
